{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>
<form class="layui-form" action="" id="cross_srh">
    <div class="proxy_srh">
        <div class="layui-inline">
            <div class="layui-inline">

                <div class="layui-input-inline">
                    <label class="layui-form-label">任务别名：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="task_name" id="task_name" placeholder="请输入"
                               style="width:212px">
                    </div>
                </div>

                <div class="layui-input-inline">
                    <label class="layui-form-label">开始时间：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="t1" id="t1" placeholder="请输入" style="width:212px">
                    </div>

                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">结束时间：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="t2" id="t2" placeholder="请输入" style="width:212px">
                    </div>

                </div>

                <div class="layui-inline" style="left: 257px">
                    <button class="layui-btn" type="button" id="start" lay-submit lay-filter="start">查询</button>
                </div>
            </div>
        </div>

    </div>
</form>
<hr class="layui-bg-gray">
<button class="layui-btn  layui-btn-normal" id="add" type="button">
    <i class="layui-icon"></i> 创建案例
</button>
<!--<button class="layui-btn layui-btn-normal layui-btn-danger" id="delete" type="button">-->
<!--<i class="layui-icon"></i> 删除-->
<!--</button>-->
<script type="text/html" id="filemange_v1">
    <button type="button"  class="layui-btn layui-btn-xs layui-btn-danger" lay-event="getNode">占用</button>

     <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delNode">
    <i class="layui-icon">&#xe640;</i>
  </button>
</script>

<table class="layui-hide" id="boomerCase" lay-filter="boomerCase"></table>
<script>
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var loading = layer.load();
          var  options = {
                elem:'#boomerCase',
                skin:"line",
                page:true,
                height: 'full-200',
                toolbar: true,
                url:'/locust/case/list?',
                limit:10,
                limits:[10,20,30],
                where:{
                    task_name : $('#task_name').val(),
                    start_time:$('#t1').val(),
                    end_time:$('#t2').val(),
                },
                response:{
                    statusName:'errorCode',
                    statusCode:'',
                    dataName:'data'
                },
                cols:[[
                      {field: 'id', title: 'id',sort:true,width:80}
                      ,{field: 'task_name', title: 'task_name',sort:true}
                      ,{field: 'master_host', title: 'master_host',sort:true,templet: function(d){
                        if(d.master_host!=""){
                            return '<a class="layui-table-link" target="_blank" href="'+ d.master_host +'">'+d.master_host+'</a>';
                        }
                        return d.master_host;
                      }}
                      ,{field: 'master_node', title: 'master_node',sort:true}
                      ,{field: 'case_json', title: 'case_json',sort:true,templet: function(d){
                            jStr =  JSON.stringify(d.case_json, null, 4);
                            return jStr;
                      }}
                      ,{field: 'last_time', title: 'last_time',sort:true}
                      ,{field: 'nodeId', title: 'nodeId',width:1}
                      ,{fixed: 'right', title:'', toolbar: '#filemange_v1',width:150}

                ]],
                id:'caseBoomer',
                done:function(res,cur,count){
                    $("[data-field='nodeId']").css('display','none');
                    layer.close(loading);
                }
              };
          var tableIns = table.render(options);
          //点击添加页面
          $('#add').on('click',function(){

                layer.open({
                            type:2,
                            content:"/load/add",
                            area:['1055px','473px'],
                            //offset: ['231px','307px'],
                            title:"创建案例",
                            closeBtn:1,
                            shade: 0,
                            shadeClose:true,maxmin: true,
                            cancel: function(index, layero){
                                 $('#start').click();
                            }
                });

          });
          table.on('row(boomerCase)', function(obj){
                obj.tr.css("background","#eeeeee");
          });
          table.on('rowDouble(boomerCase)', function(obj){
            msg = JSON.stringify(obj.data.case_json, null, 4);

            layer.open({
              title: '案例',
              area:['1055px','473px']
              ,content: '<pre>'+msg+'</pre>'
              ,closeBtn:1,
              shade: 0,
              shadeClose:true
            });

          });

          table.on('tool(boomerCase)', function(obj){

                if(obj.event === 'runLocust'){


                }else if(obj.event === 'getNode'){
                    //先查有多少个可用节点
                    if(obj.data.master_host!=""){
                        layer.msg("已分配节点，请不要再获取");
                        return;
                    }
                    $.ajax({
                        //发出请求
                        type:"GET",
                        url:"/locust/node",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        success: function(result){
                            //然后保存节点
                            if(result.data.length>0){
                                data = result.data[0];
                                nodeId = data.id;
                                master_host = data.master_host;
                                master_port = data.master_port;
                                caseId = obj.data.id;

                                $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/save/locust/node",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify({"nodeId":nodeId,"caseId":caseId}),
                                    success: function(result){
                                        layer.msg("获取成功!");
                                        obj.update({"master_host":master_host,"master_node":master_port,"nodeId":nodeId});
                                        //$('#start').click();

                                    }
                                });
                            }else{
                                layer.msg("当前无可用节点");

                            }

                        }
                    });


                }else if(obj.event==='delNode'){

                    if(obj.data.master_host=="" || obj.data.nodeId=="" ){
                        layer.msg("没有分配节点，不存在释放节点的情况");
                        return;
                    }
                    nodeId=obj.data.nodeId;
                    $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/del/locust/node",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify({"nodeId":nodeId}),
                                    success: function(result){
                                        layer.msg("释放成功!");
                                        obj.update({"master_host":"","master_node":"","nodeId":""});
                                    }
                    });


                }
          });
          //点击查询
          $('#start').on('click',function(){
              var loading = layer.load();

              options = {
                elem:'#boomerCase',
                skin:"line",
                toolbar: true,
                height: 'full-200',
                page:true,
                url:'/locust/case/list?',
                limit:10,
                limits:[10,20,30],
                where:{
                    task_name : $('#task_name').val(),
                    start_time:$('#t1').val(),
                    end_time:$('#t2').val(),
                }
              }
              tableIns.reload(options);
              layer.close(loading);
          });

          form.render();
          layer.close(loading);
    });



</script>
<script>
//时间控件
layui.use('laydate', function(){
  var laydate = layui.laydate;
  laydate.render({
    elem: '#t1',
    type:'datetime',
    format:'yyyy-MM-dd HH:mm:ss',
  });
  laydate.render({
    elem: '#t2',
    type:'datetime',
    format:'yyyy-MM-dd HH:mm:ss',
  });

});


</script>

{% endblock %}